package com.ruoyi.web.dorm.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.web.dorm.entity.StudentEntity;
import com.ruoyi.web.dorm.entity.dto.StudentDTO;
import com.ruoyi.web.dorm.entity.vo.StudentVO;
import com.ruoyi.web.dorm.mapper.StudentMapper;
import com.ruoyi.web.dorm.service.StudentService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;


    /**
     * 查询学生信息
     * @param studentDTO
     * @return
     */
    @Override
    public List<StudentVO> studentList(StudentDTO studentDTO) {
        return studentMapper.studentList(studentDTO);
    }

    /**
     * 添加学生信息
     * @param studentEntity
     * @param studentDTO
     * @return
     */
    @Override
    public int addStudent(StudentEntity studentEntity,StudentDTO studentDTO) {


        String username = SecurityUtils.getLoginUser().getUsername(); //获取创建人的名字 添加进去
        studentEntity.setCreateBy(username);

      studentMapper.addStudent(studentEntity); //添加数据

       //获取年份  id 拼接id更新进表
        Calendar calendar = Calendar.getInstance();

        // 获取当前年分
        int year = calendar.get(Calendar.YEAR);
        //获取id
        Long id = studentEntity.getId();
        String studentNum = (year+""+id);

        return  studentMapper.updateStudentNum(id,studentNum);
    }

    /**
     * 修改学生信息
     * @param studentEntity
     * @param studentDTO
     * @return
     */
    @Override
    public int updateStudent(StudentEntity studentEntity,StudentDTO studentDTO) {

        String username = SecurityUtils.getLoginUser().getUsername(); //获取修改人的名字 添加进去
        studentEntity.setUpdateBy(username);
        return studentMapper.updateStudent(studentEntity);
    }

    /**
     * 删除学生信息
     * @param id
     * @return
     */
    @Override
    public int delStudent(Long id) {
        return studentMapper.delStudent(id);
    }
}
